package javaapplication1.dao.oracleImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javaapplication1.model.PetStore;
import javaapplication1.dao.PetStoreDao;

public class PetStoreDaoOracleImpl implements PetStoreDao {

	private Connection connection = null;
	private PreparedStatement ps = null;
	private ResultSet rs = null;

	@Override
	public boolean createPetStore(PetStore petStore) {
		boolean flag = false;
		try {
			String sql = "insert into T_PETSTORE values(SEQ_T_PETSTORE.nextval,?,?,?)";
			connection = OracleConnection.getConnection();
			ps = connection.prepareStatement(sql);
			ps.setString(1, petStore.getName());
			ps.setString(2, petStore.getPassword());
			ps.setInt(3, petStore.getBalance());
			int line = ps.executeUpdate();
			flag = (line == 1) ? true : false;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			OracleConnection.closeConn(connection, ps, rs);
		}
		return flag;
	}

	@Override
	public boolean deletePetStore(int id) {
		boolean flag = false;
		try {
			String sql = "delete from T_PETSTORE where id = ?";
			connection = OracleConnection.getConnection();
			ps = connection.prepareStatement(sql);
			ps.setInt(1, id);
			int line = ps.executeUpdate();
			flag = (line == 1) ? true : false;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			OracleConnection.closeConn(connection, ps, rs);
		}
		return flag;
	}

	@Override
	public boolean updatePetStore(PetStore petStore) {
		return false;
	}
	
	@Override
	public boolean updatePetStore(int id, int balance){
		boolean flag = false;
		try {
			String sql = "update T_PETSTORE set BALANCE = ? where id = ?";
			connection = OracleConnection.getConnection();
			ps = connection.prepareStatement(sql);
			ps.setInt(1, balance);
			ps.setInt(2, id);
			int line = ps.executeUpdate();
			flag = (line == 1) ? true : false;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			OracleConnection.closeConn(connection, ps, rs);
		}
		return flag;
	}

	@Override
	public PetStore retrievePetStore(int id) {
		PetStore petStore = null;
		try {
			String sql = "select * from T_PETSTORE where id = ?";
			connection = OracleConnection.getConnection();
			ps = connection.prepareStatement(sql);
			ps.setInt(1, id);
			rs = ps.executeQuery();
			if (rs.next()) {
				petStore = new PetStore();
				petStore.setId(rs.getInt("ID"));
				petStore.setName(rs.getString("NAME"));
				petStore.setPassword(rs.getString("PASSWORD"));
				petStore.setBalance(rs.getInt("BALANCE"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			OracleConnection.closeConn(connection, ps, rs);
		}
		return petStore;
	}

	@Override
	public List<PetStore> retrievePetStoreAll() {
		List<PetStore> petStoreList = new ArrayList<PetStore>();
		try {
			String sql = "select * from T_PETSTORE";
			connection = OracleConnection.getConnection();
			ps = connection.prepareStatement(sql);
			rs = ps.executeQuery();
			if (rs.next()) {
				PetStore petStore = new PetStore();
				petStore = new PetStore();
				petStore.setId(rs.getInt("ID"));
				petStore.setName(rs.getString("NAME"));
				petStore.setPassword(rs.getString("PASSWORD"));
				petStore.setBalance(rs.getInt("BALANCE"));
				petStoreList.add(petStore);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			OracleConnection.closeConn(connection, ps, rs);
		}
		return petStoreList;
	}

}